import { mockData, type ApiResponse, type Config } from './data';

interface CreateConfigData {
  name: string;
  content: string;
}

// 配置相关的模拟API
export const configApi = {
  // 获取配置列表
  getConfigs: async (): Promise<ApiResponse<Config[]>> => {
    return {
      success: true,
      data: mockData.configs
    };
  },
  
  // 获取配置详情
  getConfig: async (id: string): Promise<ApiResponse<Config>> => {
    const config = mockData.configs.find(c => c.id === id);
    
    if (config) {
      return {
        success: true,
        data: config
      };
    }
    
    return {
      success: false,
      message: '配置不存在'
    };
  },
  
  // 创建配置
  createConfig: async (configData: CreateConfigData): Promise<ApiResponse<Config>> => {
    const newConfig: Config = {
      id: Date.now().toString(),
      ...configData,
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString()
    };
    
    mockData.configs.push(newConfig);
    
    return {
      success: true,
      data: newConfig
    };
  },
  
  // 更新配置
  updateConfig: async (id: string, configData: Partial<CreateConfigData>): Promise<ApiResponse<Config>> => {
    const index = mockData.configs.findIndex(c => c.id === id);
    
    if (index !== -1) {
      mockData.configs[index] = {
        ...mockData.configs[index],
        ...configData,
        updatedAt: new Date().toISOString()
      };
      
      return {
        success: true,
        data: mockData.configs[index]
      };
    }
    
    return {
      success: false,
      message: '配置不存在'
    };
  },
  
  // 删除配置
  deleteConfig: async (id: string): Promise<ApiResponse> => {
    const index = mockData.configs.findIndex(c => c.id === id);
    
    if (index !== -1) {
      mockData.configs.splice(index, 1);
      
      return {
        success: true
      };
    }
    
    return {
      success: false,
      message: '配置不存在'
    };
  }
};